import { defineStore } from "pinia";
import { ref } from "vue";
import router from "@/router";

export const useAuthStore = defineStore("auth", () => {
  const token = ref<string | null>(localStorage.getItem("token"));
  const isAuthenticated = ref(!!token.value);

  function login(loginToken: string) {
    token.value = loginToken;
    isAuthenticated.value = true;
    localStorage.setItem("token", loginToken);
  }

  function logout() {
    token.value = null;
    isAuthenticated.value = false;
    localStorage.removeItem("token");
    router.push("/home");
  }

  return { token, isAuthenticated, login, logout };
});
